﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Input;

using ParamSetCollection.General.Parameters.Normal;

namespace NeoReflow.UI.Example.Controls
{
    public class ExTextBox : TextBox, IParamCtrl
    {
        public ParameterUnit pu = null;
        public string m_paramTag = "";

        public ParameterUnit Pu
        {
            get { return pu; }
            set
            {
                if (pu != null)
                    pu.OnValueRenew -= Pu_OnValueRenew;
                pu = value;
                Text = pu.ViewValue;
                pu.OnValueRenew += Pu_OnValueRenew;
            }
        }
        public string ParamTag { get { return m_paramTag; } set { m_paramTag = value; } }

        public ExTextBox()
            : base()
        {


            IsReadOnly = true;
            AddHandler(TextBox.MouseLeftButtonDownEvent, new MouseButtonEventHandler(this.ExTextBox_MouseLeftButtonDown), true);
        }

        public void ConfirmParamSet()
        {
            pu.SetValue(Text);
        }




        private void ExTextBox_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ParamForm sf = new ParamForm();
            sf.InitCtrl(pu);

            sf.ShowDialog();

            if (sf.DialogResult == true)//修改完成立刻发送指令
            {
                Text = sf.ValidValue;

            }
        }

        private void Pu_OnValueRenew(string viewValue)
        {
            Text = viewValue;
        }

        public void Dispose()
        {
            pu = null;
        }
    }
}
